package weaver.interfaces.zhlx.duss.action.financemodule.sap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.SapUtil;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 暂估入账流程调用SAP暂估入账发票入账同步接口（FI092）
 * @ModifyLog:
 **/
public class WeaZgrzlcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaZgrzlcAction>>>>>requestid=" + requestid);
        try {
            String message = "调用SAP系统接口返回:";
            RecordSet rs = new RecordSet();
            SapUtil sapUtil = new SapUtil();
            String currentDate = DateUtil.getCurrentDate();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String gysdm = Util.null2String(rs.getString("gysdm"));
            String zgzje = Util.null2String(rs.getString("zgzje"));
            String zgzjebb = Util.null2String(rs.getString("zgzjebb"));
            String zftj = Util.null2String(rs.getString("zftj"));
            String rzsj = Util.null2String(rs.getString("rzsj"));
            JSONObject dataJson = new JSONObject();
            JSONObject headerJson = new JSONObject();
            headerJson.put("BUKRS", Util.null2String(rs.getString("gsdm")));
            headerJson.put("BLART", Util.null2String(rs.getString("pzlx")));
            headerJson.put("WAERS", Util.null2String(rs.getString("bzwb")));
            headerJson.put("KURSF", "");
            headerJson.put("BLDAT", rzsj.replace("-", ""));
            headerJson.put("BUDAT", rzsj.replace("-", ""));
            headerJson.put("XBLNR", Util.null2String(rs.getString("lcbh")));
            headerJson.put("XBLNR_ALT", Util.null2String(rs.getString("htlcbh")));
            headerJson.put("BKTXT", "");
            headerJson.put("BELNR", "");
            headerJson.put("USNAM", Util.null2String(rs.getString("jbrwb")));
            headerJson.put("USNAM2", Util.null2String(rs.getString("gh")));
            headerJson.put("ZDANJULEIXING", "费用暂估");
            dataJson.put("HEADER", headerJson);
            JSONArray itemList = new JSONArray();
            int count = 1;
            String dfkm = "";
            String dfkmmc = "";
            rs.executeQuery("select * from " + tablename + "_dt1 where mainid=?", mainid);
            while (rs.next()) {
                if (count == 1) {
                    dfkm = Util.null2String(rs.getString("dfkm"));
                    dfkmmc = Util.null2String(rs.getString("dfkmmc"));
                }
                JSONObject itemJson = new JSONObject();
                itemJson.put("BUZEI", count);
                itemJson.put("BSCHL", "");
                itemJson.put("HKONT", Util.null2String(rs.getString("fykm")));
                itemJson.put("ZFEIYONGLEIXING", Util.null2String(rs.getString("fykmmc")));
                itemJson.put("KUNNR", "");
                itemJson.put("LIFNR", "");
                itemJson.put("UMSKZ", "");
                itemJson.put("KOSTL", Util.null2String(rs.getString("cbzxwb")));
                itemJson.put("AUFNR", Util.null2String(rs.getString("nbddwb")));
                itemJson.put("PROJK", Util.null2String(rs.getString("wbswb")));
                itemJson.put("WRBTR", Util.null2String(rs.getString("je")));
                itemJson.put("DMBTR", Util.null2String(rs.getString("bwbje")));
                itemJson.put("MWSKZ", "");
                itemJson.put("ZFBDT", "");
                itemJson.put("ZTERM", "");
                itemJson.put("NETDT", "");
                itemJson.put("RSTGR", "");
                itemJson.put("ZUONR", Util.null2String(rs.getString("xh")));
                itemJson.put("SGTXT", "");
                itemList.add(itemJson);
                count++;
            }
            JSONObject itemJsonMain = new JSONObject();
            itemJsonMain.put("BUZEI", count);
            itemJsonMain.put("HKONT", dfkm);
            itemJsonMain.put("ZFEIYONGLEIXING", dfkmmc);
            itemJsonMain.put("LIFNR", gysdm);
            itemJsonMain.put("WRBTR", zgzje);
            itemJsonMain.put("DMBTR", zgzjebb);
            itemJsonMain.put("ZTERM", zftj);
            itemList.add(itemJsonMain);
            dataJson.put("ITEM", itemList);
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("jkdymc", "WeaZgrzlcAction");
            dataMap.put("lcid", requestid);
            dataMap.put("dylc", requestid);
            dataMap.put("cz", src);
            writeLog(">>>>>WeaZgrzlcAction>>>>>dataJson=" + dataJson);
            JSONObject resultJson = sapUtil.handleOKHttpRequest("FI092", dataJson, dataMap);
            writeLog(">>>>>WeaZgrzlcAction>>>>>resultJson=" + resultJson);
            String code = Util.null2String(resultJson.getString("code"));
            if ("success".equals(code)) {
                String output = Util.null2String(resultJson.getString("OUTPUT"));
                JSONObject outputJson = JSONObject.parseObject(output);
                String msgType = outputJson.getString("MSG_TYPE");
                String dataStr = outputJson.getString("DATA");
                JSONObject returnDataJson = JSONObject.parseObject(dataStr);
                JSONArray returnList = JSONArray.parseArray(returnDataJson.getString("RETURN"));
                if ("S".equalsIgnoreCase(msgType)) {
                    for (int i = 0; i < returnList.size(); i++) {
                        String belnr = returnList.getJSONObject(i).getString("MSG_BELNR");
                        String bukrs = returnList.getJSONObject(i).getString("MSG_BUKRS");
                        String gjahr = returnList.getJSONObject(i).getString("MSG_GJAHR");
                        String sql = "update " + tablename + " set sappzh=?,sapgsdm=?,hjnd=? where requestid=?";
                        rs.executeUpdate(sql, belnr, bukrs, gjahr, requestid);
                    }

                } else {
                    for (int j = 0; j < returnList.size(); j++) {
                        String msgType1 = returnList.getJSONObject(j).getString("MSG_TYPE");
                        if ("E".equalsIgnoreCase(msgType1)) {
                            message += returnList.getJSONObject(j).getString("MSG_TEXT")+";";
                        }
                    }
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            } else {
                message += Util.null2String(resultJson.getString("message"));
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaZgrzlcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}